<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qquickstate.cpp -->
  <title>State QML 类型 | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td ><a href="qtquick-qmlmodule.html">QML类型</a></td><td >State QML 类型</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#properties">属性</a></li>
<li class="level1"><a href="#details">详细信息</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">State QML 类型</h1>
<span class="subtitle"></span>
<!-- $$$State-brief -->
<p>Defines configurations of objects and properties. <a href="#details">更多...</a></p>
<!-- @@@State -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> 导入语句：</td><td class="memItemRight bottomAlign"> import QtQuick 2.12</td></tr></table></div><ul>
<li><a href="qml-qtquick-state-members.html">所有成员的列表，包括继承成员</a></li>
</ul>
<a name="properties"></a>
<h2 id="properties">属性</h2>
<ul>
<li class="fn"><b><b><a href="qml-qtquick-state.html#changes-prop">changes</a></b></b> : list&lt;Change&gt;</li>
<li class="fn"><b><b><a href="qml-qtquick-state.html#extend-prop">extend</a></b></b> : string</li>
<li class="fn"><b><b><a href="qml-qtquick-state.html#name-prop">name</a></b></b> : string</li>
<li class="fn"><b><b><a href="qml-qtquick-state.html#when-prop">when</a></b></b> : bool</li>
</ul>
<!-- $$$State-description -->
<a name="details"></a>
<h2 id="details">详细信息</h2>
<p>A <i>state</i> is a set of batched changes from the default configuration.</p>
<p>All items have a default state that defines the default configuration of objects and property values. New states can be defined by adding State items to the <a href="qml-qtquick-item.html#states-prop">states</a> property to allow items to switch between different configurations. These configurations can, for example, be used to apply different sets of property values or execute different scripts.</p>
<p>The following example displays a single <a href="qml-qtquick-rectangle.html">Rectangle</a>. In the default state, the rectangle is colored black. In the &quot;clicked&quot; state, a <a href="qml-qtquick-propertychanges.html">PropertyChanges</a> object changes the rectangle's color to red. Clicking within the <a href="qml-qtquick-mousearea.html">MouseArea</a> toggles the rectangle's state between the default state and the &quot;clicked&quot; state, thus toggling the color of the rectangle between black and red.</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">myRect</span>
      <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
      <span class="name">color</span>: <span class="string">&quot;black&quot;</span>

      <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> {
          <span class="name">id</span>: <span class="name">mouseArea</span>
          <span class="name">anchors</span>.fill: <span class="name">parent</span>
          <span class="name">onClicked</span>: <span class="name">myRect</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">'clicked'</span> ? <span class="name">myRect</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">&quot;&quot;</span> : <span class="name">myRect</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">'clicked'</span>;
      }

      <span class="name">states</span>: [
          <span class="type"><a href="qml-qtquick-state.html">State</a></span> {
              <span class="name">name</span>: <span class="string">&quot;clicked&quot;</span>
              <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">myRect</span>; <span class="name">color</span>: <span class="string">&quot;red&quot;</span> }
          }
      ]
  }

</pre>
<p>Notice the default state is referred to using an empty string (&quot;&quot;).</p>
<p>States are commonly used together with <a href="qtquick-statesanimations-animations.html">Transitions</a> to provide animations when state changes occur.</p>
<p><b>Note: </b>Setting the state of an object from within another state of the same object is not allowed.</p><p><b>请参考</b> <a href="qtquick-animation-example.html#states">States example</a>，<a href="qtquick-statesanimations-states.html">Qt Quick States</a>，<a href="qtquick-statesanimations-animations.html">Transitions</a> 和 <a href="../qtqml/qtqml-index.html">Qt QML</a>。</p>
<!-- @@@State -->
<h2>属性文档</h2>
<!-- $$$changes -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="changes-prop">
<td class="tblQmlPropNode"><p>
<a name="changes-prop"></a><span class="qmldefault">[default] </span><span class="name">changes</span> : <span class="type"><a href="../qtqml/qml-list.html">list</a></span>&lt;<span class="type">Change</span>&gt;</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds the changes to apply for this state</p>
<p>By default these changes are applied against the default state. If the state extends another state, then the changes are applied against the state being extended.</p>
</div></div><!-- @@@changes -->
<br/>
<!-- $$$extend -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="extend-prop">
<td class="tblQmlPropNode"><p>
<a name="extend-prop"></a><span class="name">extend</span> : <span class="type"><a href="../qtqml/qml-string.html">string</a></span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds the state that this state extends.</p>
<p>When a state extends another state, it inherits all the changes of that state.</p>
<p>The state being extended is treated as the base state in regards to the changes specified by the extending state.</p>
</div></div><!-- @@@extend -->
<br/>
<!-- $$$name -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="name-prop">
<td class="tblQmlPropNode"><p>
<a name="name-prop"></a><span class="name">name</span> : <span class="type"><a href="../qtqml/qml-string.html">string</a></span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds the name of the state.</p>
<p>Each state should have a unique name within its item.</p>
</div></div><!-- @@@name -->
<br/>
<!-- $$$when -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="when-prop">
<td class="tblQmlPropNode"><p>
<a name="when-prop"></a><span class="name">when</span> : <span class="type"><a href="../qtqml/qml-bool.html">bool</a></span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>This property holds when the state should be applied.</p>
<p>This should be set to an expression that evaluates to <code>true</code> when you want the state to be applied. For example, the following <a href="qml-qtquick-rectangle.html">Rectangle</a> changes in and out of the &quot;hidden&quot; state when the <a href="qml-qtquick-mousearea.html">MouseArea</a> is pressed:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">myRect</span>
      <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
      <span class="name">color</span>: <span class="string">&quot;red&quot;</span>

      <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">id</span>: <span class="name">mouseArea</span>; <span class="name">anchors</span>.fill: <span class="name">parent</span> }

      <span class="name">states</span>: <span class="name">State</span> {
          <span class="name">name</span>: <span class="string">&quot;hidden&quot;</span>; <span class="name">when</span>: <span class="name">mouseArea</span>.<span class="name">pressed</span>
          <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">myRect</span>; <span class="name">opacity</span>: <span class="number">0</span> }
      }
  }

</pre>
<p>If multiple states in a group have <code>when</code> clauses that evaluate to <code>true</code> at the same time, the first matching state will be applied. For example, in the following snippet <code>state1</code> will always be selected rather than <code>state2</code> when sharedCondition becomes <code>true</code>.</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-item.html">Item</a></span> {
      <span class="name">states</span>: [
          <span class="type"><a href="qml-qtquick-state.html">State</a></span> { <span class="name">name</span>: <span class="string">&quot;state1&quot;</span>; <span class="name">when</span>: <span class="name">sharedCondition</span> },
          <span class="type"><a href="qml-qtquick-state.html">State</a></span> { <span class="name">name</span>: <span class="string">&quot;state2&quot;</span>; <span class="name">when</span>: <span class="name">sharedCondition</span> }
      ]
      <span class="comment">// ...</span>
  }

</pre>
</div></div><!-- @@@when -->
<br/>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
